<?php
/**
 *  考勤
 */

class Controller_attend extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->semester = YOU::loadModel('semester');
	}


	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 列表 **/
	public function actionList()
	{
		$semester_id   = $this->get->semester_id;
		$attend_smonth = $this->get->attend_smonth;

		$semester_data = $this->model->semester->getInfo($semester_id);

		$where = " WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' ";

		

		$attend_lists = $this->db->fetchAll("SELECT * FROM `tbl_attend` {$where} ORDER BY attend_id DESC");

		// 学期列表
		$semester_lists = $this->model->semester->getIni();

		$month_lists = $this->model->semester->getMonthAll($semester_id);

		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("attend_smonth", $attend_smonth);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("month_lists", $month_lists);		
		$this->tpl->assign("attend_lists", $attend_lists);
		$this->tpl->display("attend/m.attend.list.tpl.php");
	}

	/** 添加 **/
	public function actionIn()
	{
		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if ($_POST){
			$attend_smonth = $this->post->attend_smonth;
			// 上传excel
			$data_lists = $this->model->excel->in("attend");

			if (!empty($data_lists)){
				// 删除原纪录
				$this->db->query("DELETE FROM `tbl_attend` WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}'");
			}

			foreach ($data_lists as $key => $value){
				// 写入数据库
				$info = array(
				"semester_id"   => $semester_id,
				"attend_smonth" => $attend_smonth,
				"staff_id"      => $value[1],
				"staff_name"    => $value[2],
				"attend_a"      => $value[3],
				"attend_b"      => $value[4],
				"attend_c"      => $value[5],
				"attend_d"      => $value[6],
				"attend_e"      => $value[7],
				"attend_f"      => $value[8],
				"attend_g"      => $value[9],
				"attend_h"      => $value[10],
				"attend_i"      => $value[11],
				"attend_content"=> $value[12],
				"status"        => 1,
				"create_time"   => date("Y-m-d H:i:s"),
				"update_time"   => "0000-00-00 00:00:00"

				);
				$this->db->do_insert("tbl_attend", $info);
			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=attend.list");

		}

		// 学月列表
		$month_lists = $this->model->semester->getMonthAll($semester_id);

		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("month_lists", $month_lists);
		$this->tpl->display("attend/attend.in.tpl.php");
	}

	/** 导出数据 **/
	public function actionOutData()
	{
		$semester_id   = $this->get->semester_id;
		$attend_smonth = $this->get->attend_smonth;

		$semester_data = $this->model->semester->getInfo($semester_id);

		//$where = " WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' ";

		$attend_lists = $this->db->fetchAll("SELECT * FROM `tbl_attend` {$where} ORDER BY attend_id DESC ");
		foreach ($attend_lists as $key => $value){
			$info = array(
			"0" => $value['staff_id'],
			"1" => $value['staff_name'],
			"2" => $value['attend_a'],
			"3" => $value['attend_b'],
			"4" => $value['attend_c'],
			"5" => $value['attend_d'],
			"6" => $value['attend_e'],
			"7" => $value['attend_f'],
			"8" => $value['attend_g'],
			"9" => $value['attend_h'],
			"10" => $value['attend_i'],
			"11" => $value['attend_content'],

			);
			$data_lists[$key] = $info;

		}
		// 模板标题
		$this->model->excel->excel_ini['attend']['title'] = "{$semester_data['semester_xname']}学年{$semester_data['semester_name']}学期{$attend_smonth}学月教师考勤表";
		$this->model->excel->outData("attend", $data_lists);

		$this->tpl->display();
	}

	/** 导出模板 **/
	public function actionOutTp()
	{
		// 模板标题
		$this->model->excel->excel_ini['attend']['title'] = "学年学期学月教师考勤表";


		// 导出模板 - 6行空数据
		$this->model->excel->outTp("attend");

		$this->tpl->display();
	}




}



?>